package service

import (
	"github.com/go-xorm/xorm"
	"github.com/jinzhu/gorm"
	"gowork/recruit/dbInit"
	"gowork/recruit/model"
	"gowork/recruit/utils"
	"time"
)

type PositionService struct {
	Position *model.Position
	JobSeekers *model.JobSeekers
	JobSeekersPosition *model.JobSeekersPosition
	Message *model.Message
	Session *xorm.Session
	Tx *gorm.DB
}

//func (positionService *PositionService)SubmitResume(jsp *model.JobSeekersPosition) (map[string]interface{}, error) {
//
//	w := new(utils.Worker)
//
//	positionService.Session = dbInit.X.NewSession()
//	positionService.JobSeekersPosition = new(model.JobSeekersPosition)
//	positionService.Position = new(model.Position)
//	positionService.Message = new(model.Message)
//
//	positionService.Session.Begin()
//
//	jsp.Id = w.GetId()
//	jsp.CreatedAt = time.Now()
//	jsp.UpdatedAt = time.Now()
//
//	iErr := positionService.JobSeekersPosition.InsertJobSeekersPosition(jsp, positionService.Session)
//	if iErr != nil {
//		positionService.Session.Rollback()
//
//		return map[string]interface{}{"code":500, "message":"Sql Error"}, iErr
//	}
//
//	uErr := positionService.Position.UpdatePosition(jsp.PositionId, positionService.Session)
//	if uErr != nil {
//		positionService.Session.Rollback()
//
//		return map[string]interface{}{"code":500, "message":"Sql Error"}, uErr
//	}
//
//	js, qErr := positionService.JobSeekers.QueryJobSeekers(jsp.JobSeekersId, positionService.Session)
//	if qErr != nil {
//		positionService.Session.Rollback()
//
//		return map[string]interface{}{"code":500, "message":"Sql Error"}, qErr
//	}
//	p, qErr := positionService.Position.QueryPosition(jsp.PositionId, positionService.Session)
//	if qErr != nil {
//		positionService.Session.Rollback()
//
//		return map[string]interface{}{"code":500, "message":"Sql Error"}, qErr
//	}
//	var m = model.Message{
//		Id: w.GetId(),
//		Type: 1,
//		JobSeekersId: jsp.JobSeekersId,
//		JobSeekersName: js.Name,
//		CompanyId: p.CompanyId,
//		CompanyName: p.CompanyName,
//		Content: js.Name+"已投递简历，请查看",
//		CreatedAt: time.Now(),
//		UpdatedAt: time.Now(),
//	}
//	mErr := positionService.Message.InsertMessage(&m, positionService.Session)
//	if mErr != nil {
//		positionService.Session.Rollback()
//
//		return map[string]interface{}{"code":500, "message":"Sql Error"}, mErr
//	}
//
//	jErr := positionService.JobSeekers.UpdateJobSeekers(jsp.JobSeekersId, positionService.Session)
//	if jErr != nil {
//		positionService.Session.Rollback()
//
//		return map[string]interface{}{"code":500, "message":"Sql Error"}, jErr
//	}
//
//	positionService.Session.Commit()
//
//	return map[string]interface{}{"code":200, "message":"成功"}, nil
//}


func (positionService *PositionService)SubmitResume(jsp *model.JobSeekersPosition) (map[string]interface{}, error) {

	w := new(utils.Worker)

	positionService.Tx = dbInit.Db.Begin()
	positionService.JobSeekersPosition = new(model.JobSeekersPosition)
	positionService.Position = new(model.Position)
	positionService.Message = new(model.Message)

	defer func(){
		err := recover()
		if err != nil{
			positionService.Tx.Rollback()
		}
	}()

	jsp.Id = w.GetId()
		jsp.CreatedAt = time.Now()
		jsp.UpdatedAt = time.Now()
	//positionService.JobSeekersPosition.InsertJobSeekersPosition(jsp, positionService.Tx)
	//
	//positionService.Position.UpdatePosition(jsp.PositionId, positionService.Tx)
	//
	//js, _ := positionService.JobSeekers.QueryJobSeekers(jsp.JobSeekersId, positionService.Tx)
	//p, _ := positionService.Position.QueryPosition(jsp.PositionId, positionService.Tx)

	iErr := positionService.JobSeekersPosition.InsertJobSeekersPosition(jsp, positionService.Tx)
	if iErr != nil {
		positionService.Tx.Rollback()

		return map[string]interface{}{"code":500, "message":"Sql Error"}, iErr
	}

	uErr := positionService.Position.UpdatePosition(jsp.PositionId, positionService.Tx)
	if uErr != nil {
		positionService.Tx.Rollback()

		return map[string]interface{}{"code":500, "message":"Sql Error"}, uErr
	}

	js, qErr := positionService.JobSeekers.QueryJobSeekers(jsp.JobSeekersId, positionService.Tx)
	if qErr != nil {
		positionService.Tx.Rollback()

		return map[string]interface{}{"code":500, "message":"Sql Error"}, qErr
	}
	p, qErr := positionService.Position.QueryPosition(jsp.PositionId, positionService.Tx)
	if qErr != nil {
		positionService.Tx.Rollback()

		return map[string]interface{}{"code":500, "message":"Sql Error"}, qErr
	}
	var m = model.Message{
		Id: w.GetId(),
		Type: 1,
		JobSeekersId: jsp.JobSeekersId,
		JobSeekersName: js.Name,
		CompanyId: p.CompanyId,
		CompanyName: p.CompanyName,
		Content: js.Name+"已投递简历，请查看",
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}
	mErr := positionService.Message.InsertMessage(&m, positionService.Tx)
	if mErr != nil {
		positionService.Tx.Rollback()

		return map[string]interface{}{"code":500, "message":"Sql Error"}, mErr
	}

	jErr := positionService.JobSeekers.UpdateJobSeekers(jsp.JobSeekersId, positionService.Tx)
	if jErr != nil{
		positionService.Tx.Rollback()

		return map[string]interface{}{"code":500, "message":"Sql Error"}, jErr
	}

	//positionService.Message.InsertMessage(&m, positionService.Tx)
	//
	//positionService.JobSeekers.UpdateJobSeekers(jsp.JobSeekersId, positionService.Tx)

	positionService.Tx.Commit()

	return map[string]interface{}{"code":200, "message":"成功"}, nil
}


